<?php
/**
 * Description of JqGridColumnsModel
 *
 * @author viras
 */
Yii::import('application.extensions.jqgrid.JqGridDataColumn');

class JqGridColumnsModel extends CComponent {

    public $grid;
    public $columns = array();
    public $colModel = array();
    public $colNames = array();

    public function __construct($grid=null) {
        $this->grid = $grid;
    }

    public function init(){
        foreach ($this->columns as $i => $column) {
            if (is_string($column))
                $column = $this->createDataColumn($column);
            else {
                if (!isset($column['class']))
                    $column['class'] = 'JqGridDataColumn';
                $column = Yii::createComponent($column, $this);
            }
            $this->columns[$i] = $column;
        }

        foreach ($this->columns as $column) {
            $column->init();
            $this->colModel[] = $column->toArray();
            $this->colNames[] = $column->name;
        }
    }

    /**
     * Creates a {@link CDataColumn} based on a shortcut column specification string.
     * @param string the column specification string
     * @return CDataColumn the column instance
     */
    protected function createDataColumn($text) {
        if (!preg_match('/^([\w\.]+)(:(\w*))?(:(.*))?$/', $text, $matches))
            throw new CException(Yii::t('jqgrid', 'The column must be specified in the format of "Name:Type:Label", where "Type" and "Label" are optional.'));
        $column = new JqGridDataColumn($this);
        $column->name = $matches[1];
        if (isset($matches[3]))
            $column->type = $matches[3];
        if (isset($matches[5]))
            $column->header = $matches[5];
        return $column;
    }

}
?>
